package com.ton.config;

import com.ton.interceptor.AuthInterceptor;
import com.ton.interceptor.GlobalInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
@Slf4j
public class SpringMvcConfig implements WebMvcConfigurer {

    @Autowired
    @Qualifier("globalInterceptor")
    private GlobalInterceptor globalInterceptor;

    @Autowired
    private AuthInterceptor authInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器的执行顺序addInterceptor顺序执行
        //放行列表
        List<String> list =new ArrayList<>();
        list.add("/login"); // 登录
        list.add("/register"); // 注册
        list.add("/captcha");
        list.add("/swagger-resources/**");
        list.add("/swagger-ui/**");
        list.add("/v3/**");
        list.add("/front/**");
        list.add("/doc.html");
        list.add("/webjars/**");
        list.add("/publicinterface/**");
        list.add("/admin/login"); // 放行登录接口
        list.add("/admin/edit/passwd"); // 放开一个修改密码
        list.add("/quartz/**");//定时任务管理器
        list.add("/cashier/desk/**");//收银台控制器
        list.add("/transfer/batch/**");//转账相关接口放行
        list.add("/transfer/**");//转账相关接口放行
        list.add("/order/create");// 下订单接口放行
        list.add("/order/query/**");// 订单查询放行
        list.add("/order/poll/query/**");// 订单轮询查询接口放行
        list.add("/order/link/wallet/**");// Tg钱包链接回调放行
        list.add("/order/verify/**");// 钱包支付校验
        list.add("/order/transaction/status/**");// 区块链交易确认
        registry.addInterceptor(authInterceptor).addPathPatterns("/**").excludePathPatterns(list);

    }

    /**
        静态资源放行
        @paramregistry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

       // 将所有/static/** 访问都映射到classpath:/static/ 目录下
        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");

    }
}
